/*
 * This file is part of KubeSphere Console.
 * Copyright (C) 2019 The KubeSphere Console Authors.
 *
 * KubeSphere Console is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * KubeSphere Console is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with KubeSphere Console.  If not, see <https://www.gnu.org/licenses/>.
 */

import React from 'react'
import { toJS, computed } from 'mobx'
import { observer, inject } from 'mobx-react'
import classnames from 'classnames'
import { get } from 'lodash'

import ComponentMonitoringStore from 'stores/monitoring/component'
import { Columns, Column, Loading, Icon } from '@kube-design/components'
import { Card } from 'components/Base'
import { StatusCircle } from 'components/Cards/Monitoring'
import {
  ClusterResourceStatus,
  ETCDStatus,
  ServiceComponentStatus,
} from 'clusters/components/Cards/Monitoring'

import styles from './index.scss'

//@inject('rootStore')
@observer
export default class ClusterNodeStatus extends React.Component {
  constructor(props) {
    super(props)

    const cluster = props.cluster
    this.componentMonitoringStore = new ComponentMonitoringStore({
      cluster,
    })
    this.fetchData()
  }

  get routing() {
    return this.props.rootStore.routing
  }

  get cluster() {
    return this.props.match.params.cluster
  }

  @computed
  get componentHealth() {
    return toJS(this.componentMonitoringStore.health)
  }

  @computed
  get supportETCD() {
    return this.props.rootStore.monitoring.supportETCD
  }

  componentDidMount() {
    this.updateData()
  }

  componentWillUnmount() {
    clearTimeout(this.timer)
  }

  updateData = () => {
    this.timer = setTimeout(async () => {
      await this.componentMonitoringStore.requestHealthMetrics()
      this.updateData()
    }, 2000)
  }

  getComponentStatus = component => {
    const conditions = component.conditions || []

    return conditions.some(
      item => item.status !== 'Ture' && item.type !== 'Healthy'
    )
      ? 'unhealthy'
      : 'healthy'
  }

  fetchData = () => {
    this.componentMonitoringStore.fetchHealthMetrics()
  }

  render() {
    const { counts, isLoading = false } = this.componentHealth
    const { health = 0, total = 0 } = counts.node || {}

    return (    
        
      // <Card className={styles.node} title={t('CLUSTER_NODE_STATUS')}>
        <Card className={styles.node} >      
          <StatusCircle
            theme="light"
            className={styles.nodeStatus}
            name={t('NODE_ONLINE_STATUS')}
            legend={['ONLINE_NODES', 'ALL_NODES']}
            used={health}
            total={total}
            //onClick={this.handleNodeClick}
          />
      </Card>
    )
  }
}

